<template>
  <div class="login-container">
    <div class="left">
      <div class="title-box">
        <img src="@/assets/img/login_icon.png" alt="" />
        <div class="title">黑马面面</div>
        <div class="line"></div>
        <div class="sub-title">用户登录</div>
      </div>
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        class="login-form"
      >
        <!-- prop一定不能少，有俩种作用，校验和重置 -->
        <el-form-item prop="phone">
          <el-input
            prefix-icon="el-icon-user"
            v-model="ruleForm.phone"
            placeholder="请输入手机号"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            prefix-icon="el-icon-lock"
            v-model="ruleForm.password"
            placeholder="请输入密码"
            show-password
          ></el-input>
        </el-form-item>
        <el-row :gutter="10">
          <el-col :span="16">
            <el-form-item prop="code">
              <el-input
                prefix-icon="el-icon-key"
                v-model="ruleForm.code"
                placeholder="请输入验证码"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <img
              :src="loginCaptcha"
              @click="changeloginCaptcha"
              style="width:100%;height:40px;"
              alt=""
            />
          </el-col>
        </el-row>
        <el-form-item prop="checked">
          <!-- `checked` 为 true 或 false -->
          <el-checkbox v-model="ruleForm.checked"></el-checkbox
          >&nbsp;我已阅读并同意
          <el-link type="primary" target="__blank" href="http://www.baidu.com"
            >用户协议</el-link
          >
          和
          <el-link type="primary" href="http://www.jd.com" target="__blank"
            >隐私条款</el-link
          >
        </el-form-item>
        <el-form-item>
          <el-button type="primary" style="width:100%" @click="login"
            >登录</el-button
          >
        </el-form-item>
        <el-form-item>
          <el-button type="primary" style="width:100%" @click="register"
            >注册</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <div class="right">
      <img src="@/assets/img/login_bg.png" alt="" />
    </div>
    <!-- 注册子组件 -->
    <register ref="registerRef"></register>
  </div>
</template>
<script>
import register from './register'
import { saveToken } from '@/utils/token'
export default {
  components: {
    register
  },
  data () {
    return {
      // 验证码
      loginCaptcha: process.env.VUE_APP_BASEURL + '/captcha?type=login',
      ruleForm: {
        // 看接口文档
        phone: '18511111111', // 手机号
        password: '12345678', // 密码
        code: '', // 验证码
        checked: true // 是否勾选
      },
      // 校验规则
      rules: {
        phone: [
          {
            required: true,
            validator: (rule, value, callback) => {
              // rule：规则对象，一般不用它
              // value：输入的值
              // callback：回调函数  决定校验成功还是失败 如果失败，callback(new Error('错误提示')) ,成功 callback() 不管成功还是失败都要调用
              if (!value) {
                return callback(new Error('手机号不能为空'))
              }

              // 手机号正则
              const reg = /^1[3456789][0-9]{9}$/
              if (!reg.test(value)) {
                return callback(new Error('手机号不符合规则'))
              }

              // 一定要调用  不管成功还是失败都要调用callback
              callback()
            },
            trigger: 'blur'
          }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { min: 6, max: 16, message: '密码长度为6-16个字符', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '验证码不能为空', trigger: 'blur' },
          { min: 4, max: 4, message: '验证码长度为4个字符', trigger: 'blur' }
        ],
        checked: [
          {
            required: true,
            validator: (rule, value, callback) => {
              value ? callback() : callback(new Error('请勾选用户协议'))
            },
            trigger: 'change'
          }
        ]
      }
    }
  },
  methods: {
    // 点击切换验证码
    changeloginCaptcha () {
      this.loginCaptcha =
        process.env.VUE_APP_BASEURL + `/captcha?type=login&t=${new Date() - 0}`
    },
    // 登录
    login () {
      // 全部校验
      this.$refs.ruleForm.validate(async valid => {
        // 判断验证是否通过 如果不通过则return 不执行下面的登录
        // console.log(valid)
        if (!valid) return

        const res = await this.$axios.post('/login', this.ruleForm)

        if (res.code === 200) {
          console.log(res)
          // 提示
          this.$message.success('登录成功')
          // 保存cookies
          // localStorage.setItem('heimamm67-token', res.data.token)
          saveToken(res.data.token)

          // 编程式导航跳转到layout页面
          this.$router.push('/layout')
        } else {
          this.$message.error(res.message)

          // 刷新验证码
          this.changeloginCaptcha()
        }
      })
    },
    // 弹出注册子组件
    register () {
      this.$refs.registerRef.dialogVisible = true
    }
  }
}
</script>

<style lang="less" scoped>
.login-container {
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: space-around;
  background: linear-gradient(225deg, #1493fa, #01c6fa);
  .left {
    width: 478px;
    height: 550px;
    background: #f5f5f5;
    padding: 48px;
    .title-box {
      display: flex;
      align-items: center;
      img {
        width: 22px;
        height: 17px;
        margin-right: 16px;
      }
      .title {
        font-size: 24px;
        margin-right: 14px;
      }
      .line {
        width: 1px;
        background-color: #c7c7c7;
        height: 27px;
        margin-right: 12px;
      }
      .sub-title {
        font-size: 22px;
      }
    }
    .login-form {
      margin-top: 29px;
      .el-form-item {
        margin-bottom: 22px;
      }
    }
  }
}
</style>
